package com.wft.chapter15;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class ReadFileTest {
    public static void main(String[] args) {
        File file = new File("D:\\Workspaces2018\\crazy-java\\src\\main\\java\\com\\wft\\chapter15\\ReadFileTest.java");
        try (
            FileChannel fileChannel = new FileInputStream(file).getChannel();
        ) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(256);

            Charset charset = Charset.forName("UTF-8");
            CharsetDecoder decoder = charset.newDecoder();
            while (fileChannel.read(byteBuffer) != -1){
                byteBuffer.flip();
                System.out.println(decoder.decode(byteBuffer));
                byteBuffer.clear();
                System.out.println("******************");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
